@(username: String)

$(function() {
    var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket
    var chatSocket = new WS("@routes.Application.chat(username).webSocketURL(request)")

    var sendMessage = function() {
        chatSocket.send(JSON.stringify(
            {nr: $("#PoleWprowadzania").val()}
        ))
        $("#PoleWprowadzania").val('')

    }
    
    var Send_Id_Button = function() {
      chatSocket.send(JSON.stringify(
        {nr: this.id+' '+$("#PoleWprowadzania").val() }
      ))
      $("#PoleWprowadzania").val('')
    }
    
    var GameWindow = function() {
      $("#PrzyciskiLicytacji").hide()
      $("#PrzyciskiGry").show()
      $("#PrzyciskiLobby").hide()
    }
    
    var GameAuction = function() {
      $("#PrzyciskiLicytacji").show();
      $("#Check").show();
      $("#Bet").show();
      $("#Raise").show();
      $("#Call").show();
      $("#Fold").show();
      $("#Allin").show();
      $("#Help").show();
    }
    
    var LobbyWindow = function() {
      $("#PrzyciskiLicytacji").hide()
      $("#PrzyciskiGry").hide()
      $("#PrzyciskiLobby").show()
    }
    
    function TestP(x) {
      chatSocket.send(JSON.stringify(
        {nr: x.val()}
      ))
    }

    var receiveEvent = function(event) {
        var data = JSON.parse(event.data)

        // Handle errors
        if(data.error) {
            chatSocket.close()
            $("#onError span").text(data.error)
            $("#onError").show()
            return
        } else {
            $("#onChat").show()
        }
        
        if(data.message==='CREATEH') {
          GameWindow()
          $("#StartGame").show()
          $("#ChangeCards").hide()
          $("#KolejnaRunda").hide()
        }
        else if(data.message==='CREATE') {
          GameWindow()
          $("#StartGame").hide()
          $("#ChangeCards").hide()
          $("#KolejnaRunda").hide()
        }
        else if(data.message==='WYMIANA') {
          GameWindow()
          $("#StartGame").hide()
          $("#ChangeCards").show()
          $("#KolejnaRunda").hide()
        }
        else if(data.message==='CARDS') {
          $('#DECK').empty()
          $('#DECK').append(data.deck)
        }
        else if(data.message==='BID') {
          GameWindow()
          GameAuction()
          $("#StartGame").hide()
          $("#ChangeCards").hide()
          $("#KolejnaRunda").hide()
        }
        else if(data.message==='ALLIN') {
          GameWindow()
          GameAuction()
          $("#StartGame").hide()
          $("#ChangeCards").hide()
          $("#KolejnaRunda").hide()
          $("#Check").hide()
          $("#Bet").hide()
          $("#Raise").hide()
          $("#Call").hide()
          $("#Help").hide()
        }
        else if(data.message==='ALLH') {
          GameWindow()
          $("#StartGame").hide()
          $("#ChangeCards").hide()
          $("#KolejnaRunda").show()
        }
        else if(data.message==='ALL') {
          GameWindow()
          $("#StartGame").hide()
          $("#ChangeCards").hide()
          $("#KolejnaRunda").hide()
        }
        else if(data.message==='BALL') {
          GameWindow()
          $("#StartGame").hide()
          $("#ChangeCards").show()
          $("#KolejnaRunda").hide()
        }
        else if(data.message==='LEAVE') {
          $('#DECK').empty()
          LobbyWindow()
        }
        else {
          // Create the message element       
	      var el = $('<div class="message"><p style="font-size:12px"></p></div>')
	      $("p", el).html(data.message)
	      $(el).addClass('me')
	      $('#messages').append(el)
	    }
    }

    var handleReturnKey = function(e) {
        if(e.charCode == 13 || e.keyCode == 13) {
            e.preventDefault()
            sendMessage()
        }
    }

    $("#PoleWprowadzania").keypress(handleReturnKey)
    
    document.getElementById("CreateGame").onclick = Send_Id_Button;
    document.getElementById("Join").onclick = Send_Id_Button;
    document.getElementById("Refresh").onclick = Send_Id_Button;
    
    document.getElementById("StartGame").onclick = Send_Id_Button;
    document.getElementById("LeaveGame").onclick = Send_Id_Button;
    document.getElementById("ChangeCards").onclick = Send_Id_Button;
    document.getElementById("KolejnaRunda").onclick = Send_Id_Button;
    document.getElementById("Ranking").onclick = Send_Id_Button;
    
    document.getElementById("Check").onclick = Send_Id_Button;
    document.getElementById("Bet").onclick = Send_Id_Button;
    document.getElementById("Raise").onclick = Send_Id_Button;
    document.getElementById("Call").onclick = Send_Id_Button;
    document.getElementById("Fold").onclick = Send_Id_Button;
    document.getElementById("Allin").onclick = Send_Id_Button;
    document.getElementById("Help").onclick = Send_Id_Button;


    chatSocket.onmessage = receiveEvent
    
})
